/**
 * Created by dength on 2017/04/06
 * @email 626097912@qq.com
 * 入口程序,加载启动项
 */

import React, { Component } from 'react';
import {
  View,
  Text,
  StyleSheet,
  StatusBar,
  Navigator,
  BackAndroid,
  Platform
} from 'react-native';
import { getRouteMap, registerNavigator, getNavigator } from './route/route';
import Toast from './util/toast';
import store from 'react-native-simple-store';

const lastClickTime = 0;

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  navigator: {
    flex: 1,
    backgroundColor: 'white'
  },
  errorView: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'white'
  },
  errorText: {
    color: 'red',
    fontSize: 16
  }
});

export default class Root extends Component {

  constructor(props) {
    super(props);
    this.renderScene = this.renderScene.bind(this);
    this.onBackAndroid = this.onBackAndroid.bind(this);
    this.onExitApp = this.onExitApp.bind(this);
  }

  componentDidMount() {
    //入口处初始化本地数据
    store.save('TrainIndex', {
      
    })
  }

  componentWillMount() {
    if (Platform.OS === 'android') {
      BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid);
    }
  }

  componentWillUnmount() {
    if (Platform.OS === 'android') {
      BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid);
    }
  }

  render() {
    return Platform.OS == "ios" ? (
      <Navigator
        style={styles.navigator}
        configureScene={this.configureScene}
        renderScene={this.renderScene}
        initialRoute={{
          name: 'GrabTicket',//GrabTicket
        }} />
    ) : (
        <View style={styles.container}>
          <StatusBar
            backgroundColor="#36353A"
            barStyle="light-content"
            />
          {/*跳转至初始页*/}
          <Navigator
            style={styles.navigator}
            configureScene={this.configureScene}
            renderScene={this.renderScene}
            initialRoute={{
              name: 'GrabTicket',//GrabTicket
            }} />
        </View>
      );
  }

  //出场动画: 如果RouteMap中定义了加载动画，则使用定义的动画，否则使用右侧滑入
  configureScene(route) {
    let sceneAnimation = getRouteMap().get(route.name).sceneAnimation;
    if (sceneAnimation) {
      return sceneAnimation;
    }
    //默认
    return Navigator.SceneConfigs.FloatFromRight
  }

  renderScene(route, navigator) {
    this.navigator = navigator;
    registerNavigator(navigator, true);
    let Component = getRouteMap().get(route.name).component;
    if (!Component) {
      return (
        <View style={styles.errorView}>
          <Text style={styles.errorText}>您所启动的Component未在routeMap中注册</Text>
        </View>
      );
    }
    return (
      <Component {...route} />
    );
  }

  onBackAndroid() {
    const routers = this.navigator.getCurrentRoutes();
    if (routers.length > 1) {
      this.navigator.pop();
      return true;
    }
    let now = new Date().getTime();
    if (now - lastClickTime < 2500) {//2.5秒内点击后退键两次退出应用程序
      return false;
    }
    lastClickTime = now;
    Toast.show('再按一次将退出猿动力!');
    return true;
  }

  onExitApp() {
    BackAndroid.exitApp();
  }

}
